package com.ajax;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


@WebServlet("/getAll")
public class GetAllServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //查询全部数据 List<User> list = userService.selectbyAll();

        List<User> list = new ArrayList<>();
        list.add(new User(1,"张三1","昆明1"));
        list.add(new User(2,"张三2","昆明2"));
        list.add(new User(3,"张三3","昆明3"));

        //java=>字符串
        String s = JSON.toJSONString(list);


        //处理乱码问题
        resp.setContentType("text/json;charset=utf-8");
        //resp.setContentType("application/json;charset=utf-8");

        resp.getWriter().write(s);//响应字符串


    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }
}
